﻿using System;
using System.ComponentModel;
using Loki;

namespace Luna.UI.Project
{
    public class ProjectGraphViewModel : LunaContainerViewModel
    {
        public ProjectGraphViewModel()
        {
            Init += ProjectGraphViewModel_Init;
            Load += ProjectGraphViewModel_Load;

            SourceProjects = CreateCollection<DisplayNamedEntity>();
        }

        private void ProjectGraphViewModel_Load(object P_Sender, EventArgs e)
        {
            StartDate = new DateTime(DateTime.Today.Year, 01, 01);
            EndDate = DateTime.Today;
            Synchronizer.FromProjects(SourceProjects);

            GraphChanged(this, EventArgs.Empty);
        }

        private void ProjectGraphViewModel_Init(object P_Sender, EventArgs e)
        {
            this.WatchChange(this, v => v.GraphType, v => v.GraphChanged);
            this.WatchChange(this, v => v.StartDate, v => v.PropagateValues);
            this.WatchChange(this, v => v.EndDate, v => v.PropagateValues);
            this.WatchChange(this, v => v.SelectedProjectID, v => v.PropagateValues);
        }

        #region  SelectedProjectID
        private static PropertyChangedEventArgs _SelectedProjectIDChangeArgs = ObservableHelper.CreateArgs<ProjectGraphViewModel>(x => x.SelectedProjectID);

        private Guid? _SelectedProjectID;

        public Guid? SelectedProjectID
        {
            get
            {
                return _SelectedProjectID;
            }

            set
            {
                if (value != _SelectedProjectID)
                {
                    _SelectedProjectID = value;
                    NotifyChanged(_SelectedProjectIDChangeArgs);
                }
            }
        }
        #endregion

        #region  StartDate
        private static PropertyChangedEventArgs _StartDateChangeArgs = ObservableHelper.CreateArgs<ProjectGraphViewModel>(x => x.StartDate);

        private DateTime _StartDate;

        public DateTime StartDate
        {
            get
            {
                return _StartDate;
            }

            set
            {
                if (value != _StartDate)
                {
                    _StartDate = value;
                    NotifyChanged(_StartDateChangeArgs);
                }
            }
        }
        #endregion

        #region  EndDate
        private static PropertyChangedEventArgs _EndDateChangeArgs = ObservableHelper.CreateArgs<ProjectGraphViewModel>(x => x.EndDate);

        private DateTime _EndDate;

        public DateTime EndDate
        {
            get
            {
                return _EndDate;
            }

            set
            {
                if (value != _EndDate)
                {
                    _EndDate = value;
                    NotifyChanged(_EndDateChangeArgs);
                }
            }
        }
        #endregion

        #region  GraphType
        private static PropertyChangedEventArgs _GraphTypeChangeArgs = ObservableHelper.CreateArgs<ProjectGraphViewModel>(x => x.GraphType);

        private ProjectGraphType _GraphType;

        public ProjectGraphType GraphType
        {
            get
            {
                return _GraphType;
            }

            set
            {
                if (value != _GraphType)
                {
                    _GraphType = value;
                    NotifyChanged(_GraphTypeChangeArgs);
                }
            }
        }
        #endregion

        public LokiCollection<DisplayNamedEntity> SourceProjects { get; private set; }

        #region  CurrentGraph
        private static PropertyChangedEventArgs _CurrentGraphChangeArgs = ObservableHelper.CreateArgs<ProjectGraphViewModel>(x => x.CurrentGraph);

        private ProjectGraph _CurrentGraph;

        public ProjectGraph CurrentGraph
        {
            get
            {
                return _CurrentGraph;
            }

            set
            {
                if (value != _CurrentGraph)
                {
                    _CurrentGraph = value;
                    NotifyChanged(_CurrentGraphChangeArgs);
                }
            }
        }
        #endregion

        private void GraphChanged(object P_Sender, EventArgs e)
        {
            if (CurrentGraph != null)
            {
                CurrentGraph.Dispose();
                CurrentGraph.Close.Execute(null);
            }

            CurrentGraph = null;

            switch (GraphType)
            {
                case ProjectGraphType.MonthlyCost:
                    CurrentGraph = AddNew<ProjectMonthlyCostGraphViewModel>();
                    PropagateValues(P_Sender, e);
                    break;
                default:
                    break;
            }
        }

        private void PropagateValues(object P_Sender, EventArgs e)
        {
            if (CurrentGraph != null)
            {
                CurrentGraph.StartDate = StartDate;
                CurrentGraph.EndDate = EndDate;
                CurrentGraph.ProjectID = SelectedProjectID;
            }
        }
    }
}